package cn.itcas.statistics.service.Impl;

import cn.itcas.statistics.domain.AlkalinityDosage;
import cn.itcas.statistics.domain.CODRemovalRate;
import cn.itcas.statistics.domain.DailyProfit;
import cn.itcas.statistics.domain.MedicamentConcentration;
import cn.itcas.statistics.mapper.AlkalinityDosageMapper;
import cn.itcas.statistics.mapper.CODRemovalRateMapper;
import cn.itcas.statistics.mapper.DailyProfitMapper;
import cn.itcas.statistics.mapper.MedicamentConcentrationMapper;
import cn.itcas.statistics.service.IStatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.*;

@Service
public class StatisticsService implements IStatisticsService {
    @Autowired
    private DailyProfitMapper dailyProfitMapper;

    @Autowired
    private CODRemovalRateMapper codRemovalRateMapper;

    @Autowired
    private MedicamentConcentrationMapper medicamentConcentrationMapper;

    @Autowired
    private AlkalinityDosageMapper alkalinityDosageMapper;

    @Override
    public List<DailyProfit> selectDailyProfitById(Long projectId) {
        List<DailyProfit> dailyProfitList = dailyProfitMapper.selectDailyProfitById(projectId);
        return dailyProfitList;
    }

    @Override
    public List<CODRemovalRate> selectCODRemovalRateById(Long processDataId) {
        LocalDateTime endTime = LocalDateTime.now();
        LocalDateTime startTime = endTime.minusYears(1);
        List<CODRemovalRate> codRemovalRateList = codRemovalRateMapper.selectCODRemovalRateById(processDataId,startTime,endTime);
        for (CODRemovalRate codRemovalRate : codRemovalRateList) {
            Float removalRate = (codRemovalRate.getRegulatingPool()-(codRemovalRate.getSSTOneA()+ codRemovalRate.getSSTOneB()))/ codRemovalRate.getRegulatingPool();
            codRemovalRate.setRemovalRate(removalRate*100 + "%");
        }
        return codRemovalRateList;
    }

    @Override
    public List<MedicamentConcentration> selectMedicamentById(Long projectId) {
        List<MedicamentConcentration> medicamentConcentrationList = medicamentConcentrationMapper.selectMedicamentById(projectId);
        for (MedicamentConcentration medicamentConcentration : medicamentConcentrationList) {
            medicamentConcentration.setSodiumDihydrogenPhosphateConcentration(medicamentConcentration.getSodiumDihydrogenPhosphate()/medicamentConcentration.getTreatmentWater() * 100 + "%");
            medicamentConcentration.setGlucoseConcentration(medicamentConcentration.getGlucose()/medicamentConcentration.getTreatmentWater() * 100 + "%");
            medicamentConcentration.setBioactivePhosphorusConcentration(medicamentConcentration.getBioactivePhosphorus()/medicamentConcentration.getTreatmentWater() * 100 + "%");
            medicamentConcentration.setDefoamerConcentration(medicamentConcentration.getDefoamer()/medicamentConcentration.getTreatmentWater() * 100 + "%");
            medicamentConcentration.setLiquidSodiumAcetateConcentration(medicamentConcentration.getLiquidSodiumAcetate()/medicamentConcentration.getTreatmentWater() * 100 + "%");
            medicamentConcentration.setCompositeCarbonSourceConcentration(medicamentConcentration.getCompositeCarbonSource()/medicamentConcentration.getTreatmentWater() * 100 + "%");
            medicamentConcentration.setCompoundAlkaliConcentration(medicamentConcentration.getCompoundAlkali()/medicamentConcentration.getTreatmentWater() * 100 + "%");
            medicamentConcentration.setFerrousSulfateConcentration(medicamentConcentration.getFerrousSulfate()/medicamentConcentration.getTreatmentWater() * 100 + "%");
            medicamentConcentration.setLiquidBaseConcentration(medicamentConcentration.getLiquidBase()/medicamentConcentration.getTreatmentWater() * 100 + "%");
            medicamentConcentration.setSodaAshConcentration(medicamentConcentration.getSodaAsh()/medicamentConcentration.getTreatmentWater() * 100 + "%");
        }
        return medicamentConcentrationList;
    }

    @Override
    public Map<String, Float> selectWeekAvgRate() {
        LocalDateTime endTime = LocalDateTime.now();
        LocalDateTime startTime = endTime.minusWeeks(1);
        Map<String,Float> projectWeekRate = new HashMap<>();
        // 获取所有项目部列表
        List<Long> projectIds = codRemovalRateMapper.selectProjectId();
        for (Long projectId : projectIds) {
            List<CODRemovalRate> codRemovalRateList = codRemovalRateMapper.selectCODRemovalRateById(projectId,startTime,endTime);
            Float sum = null;
            String projectName = null;
            for (CODRemovalRate codRemovalRate : codRemovalRateList) {
                String rate = codRemovalRate.getRemovalRate().replace("%", "");
                sum+=Float.parseFloat(rate);
                if(projectName == null) {
                    projectName = codRemovalRate.getProjectName();
                }
            }
            projectWeekRate.put(projectName,sum/7);
        }
        return projectWeekRate;
    }


    @Override
    public List<AlkalinityDosage> selectAlkalinityDosageById(Long projectId) {
        List<AlkalinityDosage> alkalinityDosageList = alkalinityDosageMapper.selectAlkalinityDosageById(projectId);
        for (AlkalinityDosage alkalinityDosage : alkalinityDosageList) {
            alkalinityDosage.setAlkaliAmount((alkalinityDosage.getNH3N()+alkalinityDosage.getThiocyanate()+alkalinityDosage.getTC()+alkalinityDosage.getCompoundAlkali()+alkalinityDosage.getLiquidBase()+alkalinityDosage.getSodaAsh())/alkalinityDosage.getTreatmentWater() * 100 + "%");
        }
        return alkalinityDosageList;
    }

}
